package com.zym.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.zym.entity.User;
import com.zym.enums.AckCode;
import com.zym.service.UserService;
import com.zym.utils.EasyExcel.UserDataListener;
import com.zym.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author gabriel
 * @date 2022/9/7 14:25
 */
@Api(tags = "用户excel操作")
@RestController
@RequestMapping("/excel")
public class UserController {
   @Autowired
   private UserService userService;
   @ApiOperation("导入")
    @PostMapping("/importSheet")
    public R importSheet(@RequestParam("file") MultipartFile file) {
        System.out.println("开始导入数据：" + file.getSize());
        long begin = System.currentTimeMillis();
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            //传入service用于构造方法获取后，执行数据库操作
            EasyExcel.read(inputStream, User.class, new UserDataListener(userService)).sheet().doRead();
            long end = System.currentTimeMillis();
            System.out.printf("导入完成，用时：%d秒%n", (end - begin) / 1000);
            String format = StrUtil.format("导入完成，用时：{}", (end - begin) / 1000);
            Map<String,String> map=new HashMap<>();
//            map.put("用时",format);
//            map.put("成功导入",String.valueOf(new UserDataListener().getSuccessCount()));
//            map.put("导入失败",String.valueOf(new UserDataListener().getFailList().size()));
            return R.okHasData(map);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭input流
            try {
                Objects.requireNonNull(inputStream).close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @PostMapping("/importSheets")
    public void importSheets(@RequestParam("file") MultipartFile file) {
        ExcelReader excelReader = null;
        try {
            InputStream inputStream = file.getInputStream();
            excelReader = EasyExcel.read(inputStream).build();
            ReadSheet readSheet1 = EasyExcel.readSheet(0).head(User.class).registerReadListener(new UserDataListener(userService)).build();
            ReadSheet readSheet2 = EasyExcel.readSheet(1).head(User.class).registerReadListener(new UserDataListener(userService)).build();
            excelReader.read(readSheet1, readSheet2);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (excelReader != null) {
                excelReader.finish();
            }
        }
    }

    @GetMapping("/exportSheet")
    public String exportSheet() throws IOException {
        long begin = System.currentTimeMillis();

        userService.export();

        System.out.printf("导入完成，用时：%d秒%n", (System.currentTimeMillis() - begin) / 1000);
        return String.format("导入完成，用时：%d秒%n", (System.currentTimeMillis() - begin) / 1000);
    }

//    @GetMapping("/exportSheets")
//    public String exportSheets(HttpServletResponse response) {
//        long begin = System.currentTimeMillis();
//        userService.exportSheets(response, "test02", 10000);
//        System.out.printf("导入完成，用时：%d秒%n", (System.currentTimeMillis() - begin) / 1000);
//        return String.format("导入完成，用时：%d秒%n", (System.currentTimeMillis() - begin) / 1000);
//    }
}
